package com.ewili.framework.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;

public class ObjectHttpClientUtils {

    public static Long getEastMoneyJsonPages(String baseUrl) {
        Connection connection = Jsoup.connect(baseUrl).timeout(1000000);
        connection.maxBodySize(0);
        try {
            String content = connection.get().body().text();
            if(!StringUtils.contains(content,",data:[")) {
                return Long.valueOf(0);
            }
            String subContent = StringUtils.substring(content, "{pages:",",data:[").replace("{pages:", "");
            return Long.valueOf(subContent);
        } catch (IOException e) {
            ExceptionUtils.propagate(e);
        }
        return null;
    }

    public static String[][] formatEastMoneyFormatData(String url) {
        String content = getEastMoneyJsonContent(url);
        return formatEastMoneyJsonData(content);
    }

    public static JSONArray formatEastMoneyFormatData(String url, String startRegx, String endRegx) {
        String content = getEastMoneyJsonContent(url, startRegx, endRegx);
        return formatEastMoneyJsonData(content, true);
    }

    public static String getEastMoneyJsonContent(String url) {

            String content = HttpClientUtils.getContent(url,3000000);
            int startIndex = content.indexOf("data:[\"");
            content = content.substring(startIndex).replace("]}", "").replace("data:[", "");
            return content;
    }

    public static String getEastMoneyJsonContent(String url, String startRegx, String endRegx) {
        String content = HttpClientUtils.getContent(url, 3000000);
        int startIndex = content.indexOf(startRegx);
        content = content.substring(startIndex).replace(endRegx, "]").replace(startRegx, "");
        return content;
    }

    public static String[][] formatEastMoneyJsonData(String content) {
        String[] lineConent = content.split("\",\"");
        String[][] jsonLineData = new String[lineConent.length][];
        for (int i = 0; i < lineConent.length; i++) {
            String[] rowContent = lineConent[i].split(",");
            jsonLineData[i] = new String[rowContent.length];
            for (int j = 0; j < rowContent.length; j++) {
                jsonLineData[i][j] = rowContent[j];
            }
        }
        return jsonLineData;
    }

    public static JSONArray formatEastMoneyJsonData(String content, boolean isJson) {
         return JSONArray.parseArray(content);
    }

    public static JSONObject formatEastMoneyJson(String url, String replace, int timeout) {
        String content = HttpClientUtils.getContent(url, timeout);
        return formatEastMoneyJson(content, replace);
    }

    public static JSONObject formatEastMoneyJson(String content, String replace) {
        content = content.replace(replace, "").replace(";", "");
        return JSONObject.parseObject(content);
    }

    public static JSONObject formatEastMoneyJson(String content, String startRegx, String endRegx) {
        int startIndex = content.indexOf(startRegx);
        content = content.substring(startIndex).replace(endRegx, "").replace(startRegx, "");
        System.out.println(content);
        return JSONObject.parseObject(content);
    }

}
